home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / pc / files / t_unix / j109lxa4.tar / nrhdr.c < prev    next >
C/C++ Source or Header  |  1994-06-04  |  3KB  |  139 lines

  1. /* Functions for level 3 net/rom support
  2.  * Copyright 1989 Dan Frank, W9NK
  3.  */
  4. #include "global.h"
  5. #include "mbuf.h"
  6. #include "timer.h"
  7. #include "ax25.h"
  8. #include "netrom.h"
  9. #include "lapb.h"
  10. #include <ctype.h>
  11.  
  12. /* Convert a net/rom network header to host format structure
  13.  * Return -1 if error, 0 if OK
  14.  */
  15.  
  16. int
  17. ntohnr3(hdr,bpp)
  18. register struct nr3hdr *hdr;    /* output structure */
  19. struct mbuf **bpp;
  20. {
  21.     int ttl;
  22.     
  23.     if(pullup(bpp,hdr->source,AXALEN) < AXALEN)
  24.         return -1;
  25.  
  26.     if(pullup(bpp,hdr->dest,AXALEN) < AXALEN)
  27.         return -1;
  28.  
  29.     if((ttl = PULLCHAR(bpp)) == -1)
  30.         return -1;
  31.  
  32.     hdr->ttl = ttl;
  33.  
  34.     return 0;
  35. }
  36.  
  37. /* Convert a host-format net/rom level 3 header into an mbuf ready
  38.  * for transmission.
  39.  */
  40.  
  41. struct mbuf *
  42. htonnr3(hdr)
  43. register struct nr3hdr *hdr;
  44. {
  45.     struct mbuf *rbuf;
  46.     register char *cp;
  47.  
  48.     if(hdr == (struct nr3hdr *) NULL)
  49.         return NULLBUF;
  50.  
  51.     /* Allocate space for return buffer */
  52.     if((rbuf = alloc_mbuf(NR3HLEN)) == NULLBUF)
  53.         return NULLBUF;
  54.  
  55.     rbuf->cnt = NR3HLEN;
  56.  
  57.     /* Now convert */
  58.     cp = rbuf->data;
  59.  
  60.     memcpy(cp,hdr->source,AXALEN);
  61.     cp[ALEN] &= ~E;        /* source E-bit is always off */
  62.     cp += AXALEN;
  63.     memcpy(cp,hdr->dest,AXALEN);
  64.     cp[ALEN] |= E;        /* destination E-bit always set */
  65.     cp += AXALEN;
  66.     *cp = hdr->ttl;
  67.  
  68.     return rbuf;
  69. }
  70.  
  71. /* Convert a net/rom routing broadcast destination subpacket from
  72.  * network format to a host format structure.  Return -1 if error,
  73.  * 0 if OK.
  74.  */
  75. int
  76. ntohnrdest(ds,bpp)
  77. register struct nr3dest *ds;
  78. struct mbuf **bpp;
  79. {
  80.     int quality;
  81.  
  82.     /* get destination callsign */
  83.     if(pullup(bpp,ds->dest,AXALEN) < AXALEN)
  84.         return -1;
  85.  
  86.     /* get destination alias */
  87.     if(pullup(bpp,ds->alias,ALEN) < ALEN)
  88.         return -1;
  89.     ds->alias[ALEN] = '\0';
  90.  
  91.     /* get best neighbor callsign */
  92.     if(pullup(bpp,ds->neighbor,AXALEN) < AXALEN)
  93.         return -1;
  94.  
  95.     /* get route quality */
  96.     if((quality = PULLCHAR(bpp)) == -1)
  97.         return -1;
  98.     ds->quality = uchar(quality);
  99.  
  100.     return 0;
  101. }
  102.  
  103. /* Convert a host-format net/rom destination subpacket into an
  104.  * mbuf ready for transmission as part of a route broadcast
  105.  * packet.
  106.  */
  107. struct mbuf *
  108. htonnrdest(ds)
  109. register struct nr3dest *ds;
  110. {
  111.     struct mbuf *rbuf;
  112.     register char *cp;
  113.  
  114.     if(ds == (struct nr3dest *) NULL)
  115.         return NULLBUF;
  116.  
  117.     /* Allocate space for return buffer */
  118.     if((rbuf = alloc_mbuf(NRRTDESTLEN)) == NULLBUF)
  119.         return NULLBUF;
  120.  
  121.     rbuf->cnt = NRRTDESTLEN;
  122.  
  123.     cp = rbuf->data;
  124.  
  125.     memcpy(cp,ds->dest,AXALEN);
  126.     cp += AXALEN;
  127.  
  128.     memcpy(cp,ds->alias,ALEN);
  129.     cp += ALEN;
  130.  
  131.     memcpy(cp,ds->neighbor,AXALEN);
  132.     cp += AXALEN;
  133.  
  134.     *cp = uchar(ds->quality);
  135.  
  136.     return rbuf;
  137. }
  138.  
  139.